package com.imooc.pan.server.modules.recycle.controller;

import com.google.common.base.Splitter;
import com.imooc.pan.core.constants.RPanConstants;
import com.imooc.pan.core.response.R;
import com.imooc.pan.core.utils.IdUtil;
import com.imooc.pan.server.common.utils.UserIdUtil;
import com.imooc.pan.server.modules.file.vo.RPanUserFileVo;
import com.imooc.pan.server.modules.recycle.context.DeleteContext;
import com.imooc.pan.server.modules.recycle.context.QueryRecycleFileListContext;
import com.imooc.pan.server.modules.recycle.context.RestoreContext;
import com.imooc.pan.server.modules.recycle.po.DeletePO;
import com.imooc.pan.server.modules.recycle.po.RestorePO;
import com.imooc.pan.server.modules.recycle.service.RecycleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Validated
public class RecycleController {
    @Autowired
    private RecycleService recycleService;
    @GetMapping("/recycles")
    public R<List<RPanUserFileVo>> recycles(){
        QueryRecycleFileListContext context = new QueryRecycleFileListContext();
        context.setUserId(UserIdUtil.get());
        List<RPanUserFileVo> results = recycleService.recycles(context);
        return R.data(results);
    }
    @PutMapping("/recycle/restore")
    public R restore(@Validated @RequestBody RestorePO restorePO){
        RestoreContext context = new RestoreContext();
        context.setUserId(UserIdUtil.get());
        String fileIds = restorePO.getFileIds();
        List<Long> fileIdList  = Splitter.on(RPanConstants.COMMON_SEPARATOR).splitToList(fileIds).stream().map(IdUtil::decrypt).collect(Collectors.toList());
        context.setFileIdList(fileIdList);
        recycleService.restore(context);
        return R.success();
    }
    @DeleteMapping("/recycle")
    public R delete(@Validated @RequestBody DeletePO deletePO){
        DeleteContext context = new DeleteContext();
        context.setUserId(UserIdUtil.get());
        String fileIds = deletePO.getFileIds();
        List<Long> fileIdList = Splitter.on(RPanConstants.COMMON_SEPARATOR).splitToList(fileIds).stream().map(IdUtil::decrypt).collect(Collectors.toList());
        context.setFileIdList(fileIdList);
        recycleService.delete(context);
        return R.success();
    }
}
